//
//  MineEquityTableViewCell.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/20.
//

import UIKit

class MineEquityTableViewCell: UITableViewCell {
    private lazy var backView: UIView = {
       let view = UIView()
        view.backgroundColor = .white
        view.corners([.layerMaxXMaxYCorner, .layerMinXMaxYCorner], cornerRadius: 8)
        return view
    }()
    
    private lazy var topStackView:UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal // 设置为水平排列
        stackView.distribution = .fillEqually
        stackView.spacing = 12 // 设置视图之间的间距
        return stackView
    }()
    
    lazy var subBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.titleForNormal = "订阅"
        button.titleFont = UIFont.pingFangSCMedium(ofSize: 16)
        button.backgroundColor = UIColor(hexString: "#FFAE4F")
        button.allRound(23)
        return button
    }()
    
    var gpt4 = MineEquityView()
    var sx = MineEquityView()
    var ai = MineEquityView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        self.selectionStyle = .none
        self.contentView.addSubview(backView)
        backView.addSubview(topStackView)
        backView.addSubview(subBtn)
        
        backView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.leading.trailing.equalToSuperview().inset(14)
            make.bottom.equalToSuperview()
        }
        
        topStackView.snp.makeConstraints { make in
            make.top.equalTo(25)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(68)
        }
        
        subBtn.snp.makeConstraints { make in
            make.top.equalTo(topStackView.snp.bottom).offset(20)
            make.leading.trailing.equalToSuperview().inset(33)
            make.height.equalTo(46)
        }
        
        topStackView.addArrangedSubviews([gpt4, sx, ai])
        gpt4.setup(type: 1)
        sx.setup(type: 2)
        ai.setup(type: 3)
        
        
        subBtn.setClosureTouchUpInside_k { [weak self] in
            if Appearance.shared.accoutModel != nil {
                let vc = SubscribeViewController()
                self?.parentViewController?.navigationController?.pushViewController(vc, animated: true)
            }else {
                GlobalRx.loginSubject.onNext(())
            }
        }
        
        
        gpt4.setOneTappedWithCallback_k { [weak self] in
            self?.pushChat()
        }
        
        sx.setOneTappedWithCallback_k { [weak self] in
            self?.pushCZ()
        }
        ai.setOneTappedWithCallback_k { [weak self] in
            self?.pushHH()
        }
    }
    
    func pushChat() {
        if !Appearance.shared.isLoign {
            GlobalRx.loginSubject.onNext(())
            return
        }
        if Appearance.shared.accoutModel?.userOtherInfo?.is_vip ?? 0 != 1 {
            let vc = SubscribeViewController()
            self.parentViewController?.navigationController?.pushViewController(vc, animated: true)
            return
        }
        
        let chatVc = HomeChatViewController()
        self.parentViewController?.navigationController?.pushViewController(chatVc)
    }
    
    func pushCZ() {
        if !Appearance.shared.isLoign {
            GlobalRx.loginSubject.onNext(())
            return
        }
        if Appearance.shared.accoutModel?.userOtherInfo?.is_vip ?? 0 != 1 {
            let vc = SubscribeViewController()
            self.parentViewController?.navigationController?.pushViewController(vc, animated: true)
            return
        }
        self.parentViewController?.tabBarController?.selectedIndex = 1
    }
    
    func pushHH() {
        if !Appearance.shared.isLoign {
            GlobalRx.loginSubject.onNext(())
            return
        }
        if Appearance.shared.accoutModel?.userOtherInfo?.is_vip ?? 0 != 1 {
            let vc = SubscribeViewController()
            self.parentViewController?.navigationController?.pushViewController(vc, animated: true)
            return
        }
        
        let vc = PaintingPageViewController()
        self.parentViewController?.navigationController?.pushViewController(vc)
         
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}

class MineEquityView: UIView {
   
    private lazy var iconIV: UIImageView = {
       let imageView = UIImageView()
        imageView.image = UIImage(named: "icon_Mine_更多权益")
        imageView.contentMode = .scaleToFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    private lazy var lockLb: UILabel = {
       let label = UILabel()
        label.textColor = Theme.color.text_d3
        label.font = UIFont.pingFangSCRegular(ofSize: 14)
        label.textAlignment = .center
        return label
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        self.addSubviews([iconIV, lockLb])
        iconIV.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
            make.width.height.equalTo(50)
        }
        
        lockLb.snp.makeConstraints { make in
            make.top.equalTo(iconIV.snp.bottom).offset(8)
            make.leading.trailing.equalToSuperview()
        }
    }
    
    func setup(type:Int){
        if type == 1 {
            iconIV.image = UIImage(named: "icon_mine_msg")
            lockLb.text = "上下文对话"
        }else if type == 2 {
            iconIV.image = UIImage(named: "icon_mine_ai")
            lockLb.text = "Ai创作"
        }else {
            iconIV.image = UIImage(named: "icon_mine_hh")
            lockLb.text = "Ai绘画"
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
}
